package com.zys.el.time.mq;

import com.alibaba.fastjson2.JSONObject;
import com.zys.el.time.constants.MQConstants;
import com.zys.el.time.mq.constants.ModeType;
import com.zys.el.time.mq.dto.MqDTO;
import com.zys.el.time.mq.handler.ChatHandler;
import com.zys.el.time.mq.handler.CommentHandler;
import com.zys.el.time.mq.handler.ThinkingHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息队列消费者（门面）
 */
@Component
@Slf4j
public class MQConsumer {
    @Resource
    private ChatHandler chatHandler;
    @Resource
    private ThinkingHandler thinkingHandler;
    @Resource
    private CommentHandler commentHandler;


    /**
     * 聊天记录消费者
     * @param msgData
     */
    @RabbitListener(queuesToDeclare = {@Queue(MQConstants.SAVE_CHAT_KEY)})
    public void acceptSaveMsg(JSONObject msgData) {
        log.debug("入库消息：{}，", msgData);
        // 存入db
        chatHandler.save(msgData);
    }

    /**
     * 帖子消费者
     * @param mqDTO
     */
    @RabbitListener(queuesToDeclare = {@Queue(MQConstants.THINKING_KEY)})
    public void acceptThinking(MqDTO mqDTO){
        log.debug(mqDTO.toString());
        if (mqDTO.getMode().equals(ModeType.SAVE)) {
            thinkingHandler.save(mqDTO);
        }
    }

    /**
     * 评论消费者
     * @param mqDTO
     */
    @RabbitListener(queuesToDeclare = {@Queue(MQConstants.COMMENT_KEY)})
    public void acceptComment(MqDTO mqDTO){
        log.debug(mqDTO.toString());
        if(mqDTO.getMode().equals(ModeType.SAVE)) {
            commentHandler.save(mqDTO);
        }
    }
}
